package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.config.annotation.DataScope;
import com.dragon.repository.entity.Quotes;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * 报价表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-06-20
 */
@Mapper
public interface QuotesMapper extends BaseMapper<Quotes> {

    @Select("<script>"+
            "SELECT M.* FROM " +
            "(SELECT a.*,b.customer_name AS customerName , aa.username as adminName FROM dragon_quotes a " +
            "LEFT JOIN dragon_customer b ON a.customer_id = b.id " +
            "left join dragon_admin aa on a.recode_id = aa.id " +
            " )M WHERE 1=1 and M.deleted = 0 "+
            "<if test='isContract!=null and isContract != \"\"'>"+
            " AND M.is_contract = #{isContract} " +
            "</if>"+
            "<if test='search!=null and search != \"\"'>"+
            " AND (M.quotes_name like CONCAT('%',#{search},'%'))" +
            "</if>"+
            "<if test='status!=null'>"+
            " AND M.status=#{status}" +
            "</if>"+
            "<if test='recodeId!=null'>"+
            " AND M.recode_id=#{recodeId}" +
            "</if>"+
            "ORDER BY M.id DESC "+
            "</script>")
    @DataScope(adminAlias = "M", adminField = "recode_id", deptAlias = "M", deptField = "department_id", authCodeString = "quoteManagement")
    Page<Quotes> selectQuotes(Page<Quotes> quotesPage ,String search,Integer status, Integer recodeId,Integer isContract);


    @Select("<script>"+
            "SELECT M.* FROM " +
            "(SELECT a.*,b.customer_name AS customerName , b.settlement as settlement, aa.username as adminName FROM dragon_quotes a " +
            "LEFT JOIN dragon_customer b ON a.customer_id = b.id " +
            "left join dragon_admin aa on a.recode_id = aa.id " +
            " )M WHERE 1=1 and M.deleted = 0 "+
            " AND M.is_contract = #{isContract} " +
            "<if test='search!=null and search != \"\"'>"+
            " AND (M.quotes_name like CONCAT('%',#{search},'%'))" +
            "</if>"+
            "<if test='status!=null'>"+
            " AND M.status=#{status}" +
            "</if>"+
            "<if test='recodeId!=null'>"+
            " AND M.recode_id=#{recodeId}" +
            "</if>"+
            "ORDER BY M.id DESC "+
            "</script>")
    @DataScope(adminAlias = "M", adminField = "recode_id", deptAlias = "M", deptField = "department_id", authCodeString = "quoteManagement")
    Page<Quotes> selectQuotesList(Page<Quotes> quotesPage ,String search,Integer status, Integer recodeId,Integer isContract);

    @Select("SELECT M.* FROM " +
            "(SELECT a.*,b.customer_name AS customerName,b.firstcontacter as firstcontacter, " +
            " b.telephone as telephone, b.fax as fax, aa.username as adminName FROM dragon_quotes a " +
            "LEFT JOIN dragon_customer b ON a.customer_id = b.id " +
            "left join dragon_admin aa on aa.id = a.recode_id " +
            ")M WHERE M.id = #{id} ")
    Quotes selectQuotesById(Integer id);

    /**
     * 获取报价最大单号
     * @param code
     * @return
     */
    @Select("SELECT MAX(quotes_number) FROM dragon_quotes WHERE quotes_number LIKE CONCAT(#{code},'%')")
    String maxRoundInoutNumber(String code);
}

